<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
</head>

<body>
	<script type="text/javascript">
		//1.数组转对象 {0:a,1:b..}
		var arr = ["a", "b", "c", "d"]  //输出 {0:a,1:b..}
		var obj = { ...arr }
		console.log(obj);



		//2.字符串转对象
		//思路：先截取,在分割成数组,在拼成对象
		var str = '?a=1&b=2&c=3';	//要的json :{a:1,b:2,c:3}	
		var abc = str.slice(1).split("&")
		let obj1 = {}
		abc.forEach(item => {
			// console.log(item);
			console.log(item.split("="));
			obj1[item.split("=")[0]] = item.split("=")[1]
		});
		console.log(obj1);



		//3.封装一下 

		var str1 = '?tab=1&page=2&msg=3';
		function hash2json(hash) {
			var aaa = hash.slice(1).split("&")
			let obj2 = {}
			aaa.forEach(item => {
				obj2[item.split("=")[0]] = item.split("=")[1]
			});
			console.log(obj2);
		}


		console.log(hash2json(str1)) //{tab: "1", page: "2", msg: "3"}



	</script>
</body>

</html>